package com.hp.bon.sgw.stdbss30.bizSlot;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.core.channel.SoapChannel;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.QueryBalanceResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

public class QueryBalanceBizSlot implements BizSlot {
	
    private static final Logger _log = LoggerFactory.getLogger(QueryBalanceBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();
        String queryFlag =   HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Query_Flag");
        msgBody.put("queryFlag", queryFlag);
        msgBody.put("queryItemType", "1");
        String areaCode =   HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Area_Code");
        msgBody.put("areaCode", areaCode);
        String objValue = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id");
        String objAttr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/DestinationAttr");

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);

        Map<String, String> svcObjectStruct = new HashMap<>();
        svcObjectStruct.put("objValue", objValue);
        svcObjectStruct.put("objAttr", objAttr);
        svcObjectStruct.put("objType", "3");
        svcObjectStruct.put("dataArea", queryFlag.isEmpty() ? "0" : queryFlag);
        msgBody.put("svcObjectStruct", svcObjectStruct);

        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
        _log.info("request body is {}", msgBodyStr.toString());
        _log.info("webserviceUrl is {}", url);
        String respStr = HttpClientUtils.postHttpStr(msgBodyStr,url );
        _log.info("response is {}", respStr);
        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        QueryBalanceResponse queryBalanceResponse = new Gson().fromJson(respStr, QueryBalanceResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", queryBalanceResponse.getResultCode());
        respDataXml.put("Total_Balance_Available", queryBalanceResponse.getRealBalance());

        List<QueryBalanceResponse.BalanceQuery> balanceQuery = queryBalanceResponse.getBalanceQuery();
        for (QueryBalanceResponse.BalanceQuery query : balanceQuery) {
            TreeMap<String, Object> balanceInfoXml = new TreeMap<>();
            balanceInfoXml.put("Balance_Type", query.getBalanceType());
            balanceInfoXml.put("Balance_Available", query.getAvailable());
            balanceInfoXml.put("Acct_Balance_Id", query.getAcctBalanceId());
            balanceInfoXml.put("Balance_ItemType_Detail", query.getDesc());
            balanceInfoXml.put("Balance", query.getBalance());
            balanceInfoXml.put("Balance_Used", query.getUsed());
            balanceInfoXml.put("Balance_Amount", query.getAvailable());
            balanceInfoXml.put("Eff_Date", query.getEffDate());
            balanceInfoXml.put("Exp_Date", query.getExpDate());
            balanceInfoXml.put("Cycle_Upper", query.getCycleUpper());
            balanceInfoXml.put("Cycle_Lower", query.getCycleLower());
            balanceInfoXml.put("AccNbrDetail", query.getAccNbrDetail());
            balanceInfoXml.put("State", query.getState());
            respDataXml.put("Balance_Information", balanceInfoXml);
        }
        String respXmlStr;
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }







    public static void main(String[] args) {
         String xmlBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://www.chinatelecom.cn/SGW/SOAP\">\n" +
                 "\t<soapenv:Header/>\n" +
                 "\t<soapenv:Body>\n" +
                 "\t\t<soap:SgwQueryReq>\n" +
                 "\t\t\t<MID>test@027.ctnbc-bon.net;1342445333513431;2312124</MID>\n" +
                 "\t\t\t<UID>test@027.ctnbc-bon.net</UID>\n" +
                 "\t\t\t<PWD>E807F1FCF82D132F9BB018CA6738A19F</PWD>\n" +
                 "\t\t\t<SCID>CommonDynQuery.jf3@027.ctnbc-bon.net</SCID>\n" +
                 "\t\t\t<SIG></SIG>\n" +
                 "\t\t\t<REPTAG>0</REPTAG>\n" +
                 "\t\t\t<RSV1></RSV1>\n" +
                 "\t\t\t<RSV2></RSV2>\n" +
                 "\t\t\t<DATA><![CDATA[\n" +
                 "\t\t\t\t<Service_Information>\n" +
                 "\t\t\t\t\t<Channel_Id>200</Channel_Id>\n" +
                 "\t\t\t\t\t<Area_Code>0718</Area_Code>\n" +
                 "\t\t\t\t\t<Sales_Indent_Id>19102781437|1013|202010 </Sales_Indent_Id>\n" +
                 "\t\t\t\t\t<Query_Flag>3</Query_Flag>\n" +
                 "\t\t\t\t\t<Latn_Id>1013</Latn_Id>\n" +
                 "\t\t\t\t</Service_Information>]]>\n" +
                 "\t\t\t</DATA>\n" +
                 "\t\t</soap:SgwQueryReq>\n" +
                 "\t</soapenv:Body>\n" +
                 "</soapenv:Envelope>";


        try {
            XmlMessage xmlMessage = SoapChannel.parseWSRequest(xmlBody);
            CommonDynQueryBizSlot queryBalanceBizSlot = new CommonDynQueryBizSlot();
            queryBalanceBizSlot.doBiz(xmlMessage,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

